package com.oolatina.fragments;


import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.oolatina.R;
import com.oolatina.RadioView;
import com.oolatina.SCAudioPlayer;
import com.oolatina.SCButton;
import com.oolatina.SCImage;
import com.oolatina.SCItunesPartner;
import com.oolatina.SCLabel;
import com.oolatina.SCNetwork;
import com.oolatina.activity.MainActivity;
import com.oolatina.activity.YoutubeFullscreenActivity;
import com.oolatina.activity.YoutubeView;
import com.oolatina.adapters.VideoGridAdapter;
import com.oolatina.adapters.VideoListAdapter;
import com.oolatina.views.RemoteImageView;

import android.os.AsyncTask;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class VideoListFragment extends BaseFragment implements OnItemClickListener {


	private int videoID;
	private ServerTask task;
	private ArrayList<HashMap<String, String>> videoListData;
	private ArrayList<HashMap<String, String>> mData;
	private ListView listView;
	private RemoteImageView topImage;
	private TextView topLabel;
	private VideoListAdapter adapter;
	private String youtubeID;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
	 * android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_videolist, null);
		listView = (ListView) mView.findViewById(R.id.videolistview);
		topImage = (RemoteImageView) mView.findViewById(R.id.topimage);
		topLabel = (TextView) mView.findViewById(R.id.toplabel);
		mView.findViewById(R.id.toplayout).setOnClickListener(this);
		
		Bundle extras = getArguments();
		if(extras!=null){
			videoID = extras.getInt("DATA");
		}
		
		getData();
		return mView;
	}

	private void getData() {
		String url = "http://www.oolatina.com/webservice/oolatina_api.php?command=GET_VIDEO_CATEGORIE&idcategory="+videoID;
		task = new ServerTask(this);
		task.execute(url);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		if(task!=null)
			task.cancel(true);
	}

	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		if(v.getId() == R.id.toplayout){
			Intent intent = new Intent(getActivity(), YoutubeFullscreenActivity.class);
			intent.putExtra("VIDEOID", youtubeID);
			getActivity().startActivity(intent);
		}
	}
	
	
	
	private class ServerTask extends AsyncTask<String, Void, ArrayList<HashMap<String, String>>>{
		
		private VideoListFragment mainContext;

		ServerTask(VideoListFragment mainContext){
			this.mainContext = mainContext;
		}
		
		@Override
		protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
			SCNetwork net = new SCNetwork();
			ArrayList<HashMap<String, String>> videoListData = new ArrayList<HashMap<String,String>>();
			
	        String json = net.GetJSON(params[0]);
	        try
	        {
	        	//JSONObject jsonObject = new JSONObject(json);    
	        	JSONArray jsonArray = new JSONArray(json);
	        	
	        	for(int i=0; i< jsonArray.length(); i++){
	        		HashMap<String, String> data = new HashMap<String, String>();
	                JSONObject dJson = jsonArray.getJSONObject(i);
	                data.put("title", dJson.getString("title"));
	                data.put("thumbnail", dJson.getString("thumbnail"));
	                data.put("youtube", dJson.getString("youtube"));
	                videoListData.add(data);
	        	}
	        } 
	        catch(Exception e)
	        {
	        	e.printStackTrace();
	        }
	        
			return videoListData;
		}
		
		@Override
		protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
			mainContext.onVideoData(result);
			super.onPostExecute(result);
		}
		
	}


	public void onVideoData(ArrayList<HashMap<String, String>> result) {
		mView.findViewById(R.id.progress).setVisibility(View.GONE);
		mView.findViewById(R.id.contentlayout).setVisibility(View.VISIBLE);
		mData = result;
		adapter = new VideoListAdapter(mView.getContext(), mData);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		setTopData(0);
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		setTopData(position);
	}

	private void setTopData(int position) {
		youtubeID = mData.get(position).get("youtube");
		topImage.loadImmediate(mData.get(position).get("thumbnail"));
		topLabel.setText(mData.get(position).get("title"));
	}
	
}
